package cn.vendsystem.com.service.impl;

import cn.vendcommon.com.entity.LoginLog;
import cn.vendsystem.com.mapper.LoginLogMapper;
import cn.vendsystem.com.service.LoginLogService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;


@Service
public class LoginLogServiceImpl implements LoginLogService {

    @Autowired
    private LoginLogMapper loginLogMapper;

    /**
     * 保存登录日志（事务）
     */
    @Transactional(rollbackFor = Exception.class)
    public void save(LoginLog loginLog) {
        loginLogMapper.insert(loginLog);
    }

    /**
     * 可选：基于 messageId 的幂等检查示例（如果你在表里加了 message_id 字段）
     */
    public boolean existsByMessageId(String messageId) {
        if (messageId == null) return false;
        Integer count = Math.toIntExact(loginLogMapper.selectCount(
                new QueryWrapper<LoginLog>()
                        .eq("message_id", messageId)
        ));
        return count != null && count > 0;
    }
}
